package indi.zhifa.recipe.bailan.framework.common.entity.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;

/**
 *  Description: RESTful API返回的统一包装形式
 * @Author: 芝法酱
 * @Create 2021/3/17 10:33
 */
@Slf4j
@Schema(name = "通用返回")
public class RestResponse<T>{
    @Getter
    @Schema(name = "返回数据")
    private T data;
    @Getter
    @Schema(name = "返回码，200为正常,非200即为错误")
    private int code;
    @Getter
    @Schema(name = "状态信息")
    private String message;

    public RestResponse() {
        code = HttpStatus.OK.value();
    }

    public RestResponse(T pData) {
        code = HttpStatus.OK.value();
        data = pData;
    }
    public RestResponse(int pCode, String errMsg) {
        code = pCode;
        message = errMsg;
    }
    public RestResponse(int pCode, T pData, String errMsg) {
        code = pCode;
        data = pData;
        message = errMsg;
    }
    public static <T> RestResponse<T> builderJsonResult(int code,T data,String msg){
        return new RestResponse<T>(code,data,msg);
    }

    public static <T> RestResponse<T> ok(T pData){
        return new RestResponse<T>(pData);
    }

    public static RestResponse error(String pMsgErr){
        return new RestResponse(500,pMsgErr);
    }
    public static RestResponse error(int pCode, String pMsgErr){
        return new RestResponse(pCode,pMsgErr);
    }

}
